#include "mypushbutton.h"
#include <QPixmap>
#include <QDebug>
#include <QPropertyAnimation>
#include <QRect>
//MyPushButton::MyPushButton(QWidget *parent) : QPushButton(parent)
//{

//}
//不能和 .h  和 .cpp同时拥有默认初始化值
MyPushButton::MyPushButton(QString normalImg,QString pressImg)
{
    this->normalImgPath=normalImg;
    this->pressImgPath=pressImg;
    QPixmap pix;
    bool ret=pix.load(normalImg);
    if(!ret){
        qDebug()<<"图片加载失败！";
        return;
    }
    this->setFixedSize(pix.width(),pix.height());
    //设置不规则图片的样式,去掉之后，按键会有外边框
    this->setStyleSheet("QPushButton{border:0px}");
    this->setIcon(pix);
    //设置tu'biao图标大小  要不然图标太小
    this->setIconSize(QSize(pix.width(),pix.height()));
}

void MyPushButton::zoom1(int distance)
{
    QPropertyAnimation *animation=new QPropertyAnimation(this,"geometry");
    animation->setDuration(200);
    animation->setStartValue(QRect(this->x(),this->y(),this->width(),this->height()));
    animation->setEndValue(QRect(this->x(),this->y()+distance,this->width(),this->height()));
    //设置弹跳曲线
    animation->setEasingCurve(QEasingCurve::OutBounce);
    //执行动画
    animation->start();
}

void MyPushButton::mousePressEvent(QMouseEvent *event)
{
    if (this->pressImgPath!=""){
        QPixmap pix;
        pix.load(this->pressImgPath);
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类执行其它操作
    return QPushButton::mousePressEvent(event);
}
void MyPushButton::mouseReleaseEvent(QMouseEvent *event)
{
    if (this->normalImgPath!=""){
        QPixmap pix;
        pix.load(this->normalImgPath);
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));
    }
    //让父类执行其它操作
    return QPushButton::mouseReleaseEvent(event);
}
